package com.javacoo.xservice.base.config;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.javacoo.xservice.base.interceptor.RequestHandlerInterceptor;
import com.javacoo.xservice.base.interceptor.LocaleInterceptor;
import com.javacoo.xservice.base.interceptor.MaliciousRequestInterceptor;
import com.javacoo.xservice.base.interceptor.RequestLimitInterceptor;
import com.javacoo.xservice.base.setting.AppCoreSetting;

/**
 * web配置
 * <p>说明:</p>
 * <li></li>
 * @author DuanYong
 * @since 2017年3月15日上午11:27:26
 */
@Configuration
public class WebConfig implements WebMvcConfigurer{
    /**恶意请求拦截器*/
    @Autowired
    private MaliciousRequestInterceptor maliciousRequestInterceptor;
    @Autowired
    private AppCoreSetting appCoreSetting;

    @Bean
    public MaliciousRequestInterceptor maliciousRequestInterceptor() {
        MaliciousRequestInterceptor maliciousRequestInterceptor = new MaliciousRequestInterceptor();
        maliciousRequestInterceptor.setNextInterceptor(requestLimitInterceptor());
        return maliciousRequestInterceptor;
    }

	@Bean
    public RequestLimitInterceptor requestLimitInterceptor() {
        RequestLimitInterceptor requestLimitInterceptor = new RequestLimitInterceptor();
        requestLimitInterceptor.setNextInterceptor(localeInterceptor());
        return requestLimitInterceptor;
    }

    @Bean
    public LocaleInterceptor localeInterceptor() {
        LocaleInterceptor localeInterceptor = new LocaleInterceptor();
        localeInterceptor.setNextInterceptor(handlerInterceptor());
        return localeInterceptor;
    }

    @Bean
    public RequestHandlerInterceptor handlerInterceptor() {
        return new RequestHandlerInterceptor();
    }



	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        //包含的路径
        String[] includeUrlArray = StringUtils.isNotBlank(appCoreSetting.getIncludePathPatterns()) ? appCoreSetting.getIncludePathPatterns().split(",") : new String[]{};
        //排除的路径
        String[] excludeUrlArray = StringUtils.isNotBlank(appCoreSetting.getExcludePathPatterns()) ? appCoreSetting.getExcludePathPatterns().split(",") : new String[]{};

        //恶意请求拦截器
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(maliciousRequestInterceptor);
        if(ArrayUtils.isNotEmpty(includeUrlArray)){
            interceptorRegistration.addPathPatterns(includeUrlArray);
        }
        if(ArrayUtils.isNotEmpty(excludeUrlArray)){
            interceptorRegistration.excludePathPatterns(excludeUrlArray);
        }
    }

    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 对接口配置跨域设置
        source.registerCorsConfiguration("/**", buildConfig());
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<CorsFilter>(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        // 1 设置访问源地址
        corsConfiguration.addAllowedOrigin("*");
        // 2 设置访问源请求头
        corsConfiguration.addAllowedHeader("*");
        // 3 设置访问源请求方法
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

}
